#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QPixmap>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
      , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    scene = new QGraphicsScene;
    ui->graphicsView->setScene(scene);

    connect(ui->actionchooseImage, &QAction::triggered, this, &MainWindow::chooseImage);
    connect(ui->btnConfirm, &QPushButton::clicked, this, &MainWindow::resizeImage);
    connect(ui->actionresetPic, &QAction::triggered, this, [=]() {
        if (img) {
            drawImage(*img);
        }
    });
    connect(ui->actionsavePic, &QAction::triggered, this, &MainWindow::saveImage);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::reset()
{
    scene->clear();
    delete img;
    ui->WidthLine->setText("0");
    ui->HeightLine->setText("0");
}

void MainWindow::chooseImage()
{
    QString img_path = QFileDialog::getOpenFileName(this,
                                                    tr("选择图片"),
                                                    "./",
                                                    tr("Image Files(*.jpg *.png)"));
    if (img_path == "")
        return;

    reset();
    img = new QImage;
    if (!(img->load(img_path))) {
        QMessageBox::critical(this, tr("错误"), tr("图像文件无法打开！"));
        delete img;
        return;
    }
    drawImage(*img);
}

void MainWindow::drawImage(QImage image)
{
    scene->clear();

    ui->WidthLine->setText(QString::number(image.width()));
    ui->HeightLine->setText(QString::number(image.height()));

    QPixmap img_pixmap = QPixmap::fromImage(image);
    pic_item = new QGraphicsPixmapItem;
    pic_item->setPixmap(img_pixmap);
    pic_item->setPos(0, 0);
    scene->setSceneRect(pic_item->sceneBoundingRect()); // 让场景的边框等于图片边框，即居中
    scene->addItem(pic_item);
}

void MainWindow::saveImage()
{
    if (!img)
        return;

    QString path = QFileDialog::getSaveFileName(this,
                                                tr("存储图像"),
                                                ".",
                                                tr("Image Files(*.jpg *.png *.bmp *.gif)"));
    if (path == "")
        return;

    if (!(pic_item->pixmap().save(path))) //保存图像
    {
        QMessageBox::critical(this, tr("错误"), tr("存储图像失败!"));
    }
}

void MainWindow::resizeImage()
{
    if (!img)
        return;

    switch (ui->comboBox->currentIndex()) {
    case 0:
        toSOneInch();
        break;
    case 1:
        toOneInch();
        break;
    case 2:
        toLOneInch();
        break;
    case 3:
        toSTwoInch();
        break;
    case 4:
        toTwoInch();
        break;
    case 5:
        toGivenInch();
        break;
    default:
        break;
    }
}

void MainWindow::toSOneInch()
{
    QImage image = img->scaled(259, 377, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    drawImage(image);
}

void MainWindow::toOneInch()
{
    QImage image = img->scaled(295, 413, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    drawImage(image);
}

void MainWindow::toLOneInch()
{
    QImage image = img->scaled(390, 567, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    drawImage(image);
}

void MainWindow::toSTwoInch()
{
    QImage image = img->scaled(413, 531, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    drawImage(image);
}

void MainWindow::toTwoInch()
{
    QImage image = img->scaled(413, 626, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    drawImage(image);
}

void MainWindow::toGivenInch()
{
    QImage image = img->scaled(ui->WidthLine->text().toInt(),
                               ui->HeightLine->text().toInt(),
                               Qt::KeepAspectRatio,
                               Qt::SmoothTransformation);
    drawImage(image);
}
